/*
 * 
 */
package com.flying.dcss.userpermissions.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;

import com.flying.dcss.app.SessionName;
import com.flying.dcss.userpermissions.bo.impl.DcssAgentInfo;
import com.flying.dcss.userpermissions.bo.impl.DcssUser;
import com.flying.dcss.userpermissions.service.impl.AgentInfoService;
import com.flying.dcss.userpermissions.service.impl.UserService;
import com.opensymphony.xwork2.ActionSupport;

// TODO: Auto-generated Javadoc
/**
 * The Class PostSetAgentAction.
 */
public class PostSetAgentAction extends ActionSupport {
	
	/** The user service. */
	private UserService userService;
	
	/** The agent info service. */
	private AgentInfoService agentInfoService;

	/**
	 * Sets the user service.
	 *
	 * @param userService the new user service
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * Sets the agent info service.
	 *
	 * @param agentInfoService the new agent info service
	 */
	public void setAgentInfoService(AgentInfoService agentInfoService) {
		this.agentInfoService = agentInfoService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		String dcId = ServletActionContext.getRequest().getParameter("dcId");
		String agentEmail = ServletActionContext.getRequest().getParameter(
				"agentEmail");

		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date deadline = format.parse(ServletActionContext.getRequest()
				.getParameter("deadline"));

		String reason = ServletActionContext.getRequest()
				.getParameter("reason");

		// Get current user.
		DcssUser user = (DcssUser) ServletActionContext.getRequest()
				.getSession().getAttribute(SessionName.UserSession);
		String userId = user.getId();

		String agentId = userService.getUserByEmail(agentEmail).getId();

		DcssAgentInfo agentInfo = new DcssAgentInfo(UUID.randomUUID()
				.toString(), userId, agentId, deadline, reason, dcId);

		agentInfoService.AddAgentInfo(agentInfo);
		return this.SUCCESS;
	}
}
